﻿<%@ WebHandler Language="C#" Class="postback" %>

using System;
using System.Web;

public class postback : IHttpHandler
{

    public void ProcessRequest(HttpContext context)
    {
        //context.Response.ContentType = "text/x-json";
        context.Response.ContentType = "application/x-json;utf-8";
        ResponseWrite_JSON(context);
    }

    public bool IsReusable
    {
        get
        {
            return true;
        }
    }

    private void ResponseWrite_JSON(HttpContext context)
    {
        string page = context.Request.Form["page"];
        string rp = context.Request.Form["rp"];
        string sortname = context.Request.Form["sortname"];
        string sortorder = context.Request.Form["sortorder"];
        string query = context.Request.Form["query"];
        string qtype = context.Request.Form["qtype"];

        string sort = "";
        string where = "";

        if (string.IsNullOrWhiteSpace(sortname)) sortname = "name";
        if (string.IsNullOrWhiteSpace(sortorder)) sortorder = "desc";

        sort = string.Format("ORDER BY {0} {1}", sortname, sortorder);

        if (string.IsNullOrWhiteSpace(page)) page = "1";
        if (string.IsNullOrWhiteSpace(rp)) rp = "10";

        if (!string.IsNullOrWhiteSpace(query)) where = string.Format(" WHERE {0} LIKE '%{1}%' ", qtype, query);

        string json = "";
        json += "{\n";
        json += "page: 2,\n";
        json += "total: 22,\n";
        json += "rows: [";

        bool rc = false;

        for (int i = 1; i < 20; i++)
        {
            if (rc) json += ",";
            
            json += "\n{";
            json += "id:'" + i + "'";
            json += ",cell:['cell" + i.ToString() + "'";
            json += ",'name" + i.ToString() + "'";
            json += ",'printable_name" + i.ToString() + "'";
            json += ",'iso3" + i.ToString() + "'";
            json += ",'" + i.ToString() + "']";
            json += "}";
            
            rc = true;
        }

        json += "]\n";
        json += "}";

        context.Response.Write(json);
    }

}